package cn.qsgame.base;

import android.content.Context;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * @author #Suyghur.
 * Created on 4/22/21
 */
final class SdkBridgeManager {
    static Object sdkBridge = null;
    static Class<?> clzBridge = null;

    public static Object initSdkBridgeManager(Context context) {
        if (sdkBridge != null) {
            return sdkBridge;
        }

        try {
            clzBridge = Class.forName("cn.qsgame.comm.SdkBridge");
            //获取构造函数的构造器
            Constructor<?> constructor = clzBridge.getDeclaredConstructor(Context.class);
            sdkBridge = constructor.newInstance(context);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sdkBridge;
    }

    public static void call(String methodName, Class<?>[] types, Object[] values) {
        try {
            Method method = clzBridge.getMethod(methodName, types);
            method.invoke(sdkBridge, values);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Object callback(String methodName, Class<?>[] types, Object[] values) {
        try {
            Method method = clzBridge.getMethod(methodName, types);
            return method.invoke(sdkBridge, values);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
